/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

@import '../commons/index.css';

:root {
  --spectrum-textfield-padding-top: 3px;
  --spectrum-textfield-padding-bottom: 5px;

  /* Todo fix in DNA */
  --spectrum-textfield-quiet-border-radius: 0;

  --spectrum-textfield-quiet-invalid-background-position: 100% 50%;

  --spectrum-textfield-multiline-height: auto;
  --spectrum-textfield-multiline-min-height: var(--spectrum-global-dimension-size-700);

  --spectrum-textfield-multiline-padding-x: var(--spectrum-textfield-padding-x);
  --spectrum-textfield-multiline-padding-top: var(--spectrum-textfield-padding-top);
  --spectrum-textfield-multiline-padding-bottom: var(--spectrum-textfield-padding-bottom);

  --spectrum-textfield-border-size: var(--spectrum-alias-input-border-size);
  --spectrum-textfield-quiet-border-size: var(--spectrum-alias-input-border-size);
}

.spectrum-Textfield {
  composes: spectrum-FocusRing;

  display: inline-flex;
  position: relative;
  min-width: var(--spectrum-textfield-min-width);
  width: var(--spectrum-component-single-line-width);

  &.spectrum-Textfield--quiet.spectrum-Textfield--multiline .spectrum-Textfield-input {
    height: var(--spectrum-textfield-height);
    min-height: var(--spectrum-textfield-height);
    overflow-x: hidden;
  }
}

.spectrum-Textfield--quiet {
  composes: spectrum-FocusRing--quiet;
}

.spectrum-Textfield-input {
  /* box */
  box-sizing: border-box;
  border: var(--spectrum-textfield-border-size) solid;
  border-radius: var(--spectrum-textfield-border-radius);

  /* Apply padding by default to center text, giving consistency between input and textfield */
  padding: var(--spectrum-textfield-padding-top) var(--spectrum-textfield-padding-x) var(--spectrum-textfield-padding-bottom) calc(var(--spectrum-textfield-padding-x) - 1px);
  /* Use padding instead of text-indent because text-indent does not left align the text in Edge browser  */
  text-indent: 0;

  inline-size: 100%;
  height: var(--spectrum-textfield-height);

  vertical-align: top; /* used to align them correctly in forms. */

  /* Remove the margin for input in Firefox and Safari. */
  margin: 0;

  /* Show the overflow for input in Edge. */
  overflow: visible;

  /* Change the input font styles in all browsers */
  composes: i18nFontFamily;
  font-size: var(--spectrum-textfield-text-size);
  line-height: var(--spectrum-textfield-height);
  text-overflow: ellipsis;

  transition: border-color var(--spectrum-global-animation-duration-100) ease-in-out,
              box-shadow var(--spectrum-global-animation-duration-100) ease-in-out;

  outline: none;

  -webkit-appearance: none;
  /*
    Removes the native spin buttons in Firefox; -moz-appearance: none results in spinners.
    This has to come after -webkit-appearance or it gets overridden (#214)
    Details: http://stackoverflow.com/questions/23372903/hide-spinner-in-input-number-firefox-29

    Sets the opacity to 1 as normalize.css sets an opacity to placeholders
    Details: https://github.com/csstools/normalize.css/blob/master/normalize.css#L297
  */
  -moz-appearance: textfield;

  &::placeholder,
  &.is-placeholder {
    font-weight: var(--spectrum-textfield-placeholder-text-font-weight);
    font-style: var(--spectrum-textfield-placeholder-text-font-style);
    font-synthesis: none;
    transition: color var(--spectrum-global-animation-duration-100) ease-in-out;
    opacity: 1;
  }

  /* added to work with Edge, note, it needs double ::
   * not single : which is what autoprefixer will add
   */
  &::-ms-input-placeholder {
    font-weight: var(--spectrum-textfield-placeholder-text-font-weight);
    font-style: var(--spectrum-textfield-placeholder-text-font-style);
    transition: color var(--spectrum-global-animation-duration-100) ease-in-out;
    opacity: 1;
  }

  /* placeholder gets clipped for synthetic italics, we rely on font-synthesis once
   * chrome supports it https://bugs.chromium.org/p/chromium/issues/detail?id=509989
   */
  &:lang(ja), &:lang(zh), &:lang(ko), &:lang(ar), &:lang(he) {
    &::placeholder,
    &.is-placeholder {
       font-style: normal;
    }
    &::-ms-input-placeholder { /* added to work with Edge, same as above */
       font-style: normal;
    }
  }

  &:hover {
    &::placeholder {
      font-weight: var(--spectrum-textfield-placeholder-text-font-weight);
    }
  }

  &:disabled {
    /* Disable the resize functionality when disabled */
    resize: none;

    /* The opacity must be set to 1 */
    opacity: 1;
    &::placeholder,
    &.is-placeholder {
      font-weight: var(--spectrum-textfield-placeholder-text-font-weight);
    }
  }

  /* Remove the native clear button in IE */
  /* http://stackoverflow.com/questions/14007655/remove-ie10s-clear-field-x-button-on-certain-inputs */
  &::-ms-clear {
    width: 0;
    height: 0;
  }

  /* removes the native spin buttons */
  /* http://stackoverflow.com/questions/23372903/hide-spinner-in-input-number-firefox-29 */
  &::-webkit-inner-spin-button,
  &::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }

  /* removes the red border that appears in Firefox */
  &:-moz-ui-invalid {
    box-shadow: none;
  }

  .spectrum-Textfield--multiline & {
    height: var(--spectrum-textfield-multiline-height);
    min-height: var(--spectrum-textfield-multiline-min-height);
    padding: var(--spectrum-textfield-multiline-padding-top) var(--spectrum-textfield-multiline-padding-x) var(--spectrum-textfield-multiline-padding-bottom) calc(var(--spectrum-textfield-multiline-padding-x) - 1px);
    resize: none;

    /* Remove the default vertical scrollbar for textarea in IE. */
    overflow: auto;
  }

  .spectrum-Textfield--quiet & {
    border-radius: var(--spectrum-textfield-quiet-border-radius);
    border-width: 0 0 var(--spectrum-textfield-quiet-border-size) 0;

    /* removes the side padding to align the text properly */
    padding-inline-start: var(--spectrum-textfield-quiet-padding-x);
    padding-inline-end: var(--spectrum-textfield-quiet-padding-x);
  }

  .spectrum-Textfield--valid & {
    padding-inline-end: calc(var(--spectrum-textfield-padding-x) + var(--spectrum-icon-checkmark-medium-width) + var(--spectrum-textfield-icon-margin-left));
  }

  .spectrum-Textfield--invalid & {
    padding-inline-end: calc(var(--spectrum-textfield-padding-x) + var(--spectrum-icon-alert-medium-width) + var(--spectrum-textfield-icon-margin-left));
  }

  .spectrum-Textfield--loadable & {
    padding-inline-end: calc(var(--spectrum-textfield-padding-x) + var(--spectrum-icon-alert-medium-width) + var(--spectrum-textfield-icon-margin-left));
  }
}

.spectrum-Textfield-wrapper.spectrum-Textfield-wrapper--quiet{
  grid-template-rows: auto;
}

.spectrum-Textfield-wrapper .spectrum-Textfield--multiline {
  /* when textarea is inside a label wrapper, make it flex to take up remaining height. */
  flex: 1 1 auto;
  min-height: 0;

  .spectrum-Textfield-input {
    line-height: var(--spectrum-textfield-text-line-height);
  }

  &.spectrum-Textfield--quiet {
    /* quiet textfields grow based on text content, up to the height of the container. */
    /* but we need to keep help text next to input */
    flex: 0 1 auto;
    max-height: 100%;

    .spectrum-Textfield-input {
      /* make textarea scroll when reaching height of container */
      max-height: 100%;
    }
  }
}

.spectrum-Textfield-validationIcon {
  /* TODO: Confirm if this is ok for the validation icon sizing
    Note that the sizes are a bit different when compared with old background icons(more noticable for checkmark)
  */
  box-sizing: content-box;
  position: absolute;
  padding-inline-end: calc(var(--spectrum-textfield-padding-x, var(--spectrum-global-dimension-size-150)) / 2);
  pointer-events: all;
  transition: color var(--spectrum-global-animation-duration-100) ease-in-out;

  .spectrum-Textfield--quiet & {
    padding-inline-end: 0;
  }
}

.spectrum-Textfield--invalid .spectrum-Textfield-validationIcon {
  width: var(--spectrum-icon-alert-medium-width);
  height: var(--spectrum-icon-alert-medium-height);
  top: calc(calc(var(--spectrum-textfield-icon-frame) / 2) - calc(var(--spectrum-icon-alert-medium-height) / 2));
  inset-inline-end: calc(calc(var(--spectrum-textfield-icon-frame) / 2) - calc(var(--spectrum-icon-alert-medium-width) / 2));
}
.spectrum-Textfield--valid .spectrum-Textfield-validationIcon {
  width: var(--spectrum-icon-checkmark-medium-width);
  height: var(--spectrum-icon-checkmark-medium-height);
  top: calc(calc(var(--spectrum-textfield-icon-frame) / 2) - calc(var(--spectrum-icon-checkmark-medium-height) / 2));
  inset-inline-end: var(--spectrum-global-dimension-size-150);
}

/* styles the left icon for textfield, assumes usage of workflow icon sizing (18px by 18px) */
.spectrum-Textfield .spectrum-Textfield-icon {
  display: block;
  position: absolute;
  height: var(--spectrum-icon-info-medium-height);
  width: var(--spectrum-icon-info-medium-width);
  /* This has a named variable in a future update of spectrum-css. */
  inset-inline-start: var(--spectrum-global-dimension-size-150);
  top: var(--spectrum-global-dimension-size-85);
  transition: fill var(--spectrum-global-animation-duration-100) ease-in-out;
}

/* styles the textfield properly if the left icon is provided */
.spectrum-Textfield-input.spectrum-Textfield-inputIcon {
  /* Use padding instead of text-indent so long strings don't overlap the icon */
  /* These values have real names in a spectrum-css update, when we update, use those. */
  padding-inline-start: calc(
    var(--spectrum-global-dimension-size-150) +
    var(--spectrum-global-dimension-size-225) +
    var(--spectrum-global-dimension-size-65)
  );

  .spectrum-Textfield--quiet & {
    padding-inline-start: calc(var(--spectrum-global-dimension-size-100) + var(--spectrum-icon-info-medium-width));

    /* Since quiet button has no left padding, push the icon all the way to the left */
    & ~ .spectrum-Textfield-icon {
      inset-inline-start: 0;
    }
  }

  &.spectrum-Textfield--multiline {
    height: var(--spectrum-textfield-height);
    min-height: var(--spectrum-textfield-height);
  }
}


/* same positioning as invalid icon */
.spectrum-Textfield--loadable .spectrum-Textfield-circleLoader {
  position: absolute;
  top: calc(calc(var(--spectrum-textfield-icon-frame) / 2) - calc(var(--spectrum-loader-circle-small-width) / 2));
  /* can't use padding right since it breaks the circle loader so add here */
  inset-inline-end: calc(calc(calc(var(--spectrum-textfield-icon-frame) / 2) - calc(var(--spectrum-icon-alert-medium-width) / 2)) + calc(var(--spectrum-textfield-padding-x, var(--spectrum-global-dimension-size-150)) / 2));

  .spectrum-Textfield--quiet& {
    padding-inline-end: 0;
  }
}
